package study.repository;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import study.model.Study;

public class StudyJdbcRepository implements StudyRepository {
	private JdbcTemplate jdbcTemplate;
	private DataSource dataSource;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	@Override
	public Study findOne(int id){
		return jdbcTemplate.queryForObject("SELECT * FROM example WHERE id = ?", new Object[]{id}, new RowMapper<Study>(){
			@Override
			public Study mapRow(ResultSet rs, int arg1) throws SQLException {
				Study study = new Study();
				study.setId(rs.getInt("id"));
				study.setName(rs.getString("name"));
				
				return study;
			}
		});
	}
}
